/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cse.p2p.parser;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * This class contains all infomation of a file. This info will be sent from the 
 * client to the server before uploading a <code>Host File</code>
 * 
 * @author TrungHieu
 */
public class FileMetadata {

	private String fileName;
	private String fileDesc;
	private String fileHash;
    private long fileSize;
	private static final String KEY_FILE_NAME = "file_name";
	private static final String KEY_FILE_DESC = "file_desc";
	private static final String KEY_FILE_HASH = "file_hash";
    private static final String KEY_FILE_SIZE = "file_size";

	public FileMetadata( String fileName, String fileDesc, String fileHash , long fileSize) {
		this.fileName = fileName;
		this.fileDesc = fileDesc;
		this.fileHash = fileHash;
        this.fileSize = fileSize;
	}

	public String getFileDesc() {
		return fileDesc;
	}

	public String getFileHash() {
		return fileHash;
	}

	public String getFileName() {
		return fileName;
	}

    public long getFileSize() {
		return fileSize;
	}
    
	@Override
	public String toString() {
		try {
			JSONObject jsObj = new JSONObject();
			jsObj.put( KEY_FILE_NAME, this.fileName );
			jsObj.put( KEY_FILE_DESC, this.fileDesc );
			jsObj.put( KEY_FILE_HASH, this.fileHash );
            jsObj.put( KEY_FILE_SIZE, this.fileSize );
			return jsObj.toString();
		}
		catch ( JSONException e ) {
			return "";
		}
	}

	public static FileMetadata fromString( String input ) {
		try {
			JSONObject jsObj = new JSONObject( input );
			return new FileMetadata( jsObj.getString( KEY_FILE_NAME ),
									 jsObj.getString( KEY_FILE_DESC ),
									 jsObj.getString( KEY_FILE_HASH ),
                                     jsObj.getInt( KEY_FILE_SIZE ));
		}
		catch ( JSONException e ) {
			return null;
		}
	}
	
}
